package pro.lingwu.station.config;

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author @lingwu
 * @date created in 11/27/2021
 */
@Configuration
public class CorsConfig {

    /**
     * 配置跨域请求的配置
     *
     * @return 设置允许跨域的Bean
     */
    @Bean
    public FilterRegistrationBean<CorsFilter> corsFilter(){
        UrlBasedCorsConfigurationSource source
                = new UrlBasedCorsConfigurationSource();
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.addAllowedHeader("*");
        configuration.addAllowedOrigin("*");
        configuration.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", configuration);
        FilterRegistrationBean<CorsFilter> corsFilter =
                new FilterRegistrationBean<>(new CorsFilter(source));
        corsFilter.setOrder(0);
        return corsFilter;
    }

}
